﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Boomerang.Common.Configuration;
using Boomerang.Management.UIControlers;
using Boomerang.Management.UIForms;
using System.Threading;

namespace Boomerang.Management
{
    public partial class MainForm : Form
    {
        ConfigurationGridViewController gridController;

        public MainForm()
        {
            InitializeComponent();

            gridController = new ConfigurationGridViewController(dataGridView1);

            serviceControllerBoomerang.MachineName = Environment.MachineName;

            UpdateServiceStatus();
        }

        #region Commands

        //private void Save()
        //{
        //    if (MessageBox.Show(this, "Previous Configuration will be ovewriten", "Confirm", MessageBoxButtons.OKCancel)
        //        != DialogResult.Cancel)
        //    {
        //        ConfigurationManager.Save();
        //        
        //    }
        //}

        private void New()
        {
            OrganizationWizard orgForm = new OrganizationWizard();
            orgForm.Show();
        }

        private void Delete()
        {
            if (MessageBox.Show(this, "Are you sure you wan to delete ?", "Confirm", MessageBoxButtons.OKCancel)
                    != DialogResult.Cancel)
            {
                if (dataGridView1.SelectedRows.Count <= 0) return;
                Organization org = gridController.GetSelected();

                ConfigurationManager.RemoveOrganization(org);
            }
        }

        private void Edit()
        {
            if (dataGridView1.SelectedRows.Count <= 0) return;
            Organization org = gridController.GetSelected();

            OrganizationWizard orgForm = new OrganizationWizard(org);
            orgForm.Show();
        }

        #endregion

        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            New();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            New();
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void deleteToolStripButton_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void toolStripMenuItemDelete_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //if (gridController.ConfigurationChanged)
            //{
            //    if (MessageBox.Show(this, "There are unsaved configuration changes. \n Exit without saving?", "Confirm", MessageBoxButtons.YesNo)
            //        != DialogResult.Yes)
            //        e.Cancel = true;
            //}
        }


        #region Service Control
        private void toolStripButtonDeActivate_Click(object sender, EventArgs e)
        {
            try
            {
                serviceControllerBoomerang.Stop();
                FormLoading loading = new FormLoading("Stoping the service");
                loading.Worker.DoWork += new DoWorkEventHandler(WaitForService);
                loading.Worker.RunWorkerAsync(System.ServiceProcess.ServiceControllerStatus.Stopped);
                loading.ShowDialog(this);
                if (loading.Result.Error != null)
                    ExceptionBox.Show(this, loading.Result.Error);
            }
            catch (Exception) { }
            UpdateServiceStatus();
        }

        private void toolStripButtonActivate_Click(object sender, EventArgs e)
        {
            try
            {
                serviceControllerBoomerang.Start();
                FormLoading loading = new FormLoading("Starting the service");
                loading.Worker.DoWork += new DoWorkEventHandler(WaitForService);
                loading.Worker.RunWorkerAsync(System.ServiceProcess.ServiceControllerStatus.Running);
                loading.ShowDialog(this);
                if (loading.Result.Error != null)
                    ExceptionBox.Show(this, loading.Result.Error);
            }
            catch (Exception) { }
            UpdateServiceStatus();
        }

        void WaitForService(object sender, DoWorkEventArgs e)
        {
            serviceControllerBoomerang.WaitForStatus((System.ServiceProcess.ServiceControllerStatus)e.Argument,new TimeSpan(0,1,0));
        }

        private void UpdateServiceStatus()
        {
            try
            {
                if (serviceControllerBoomerang.Status != System.ServiceProcess.ServiceControllerStatus.Running)
                    LabelServiceStatus.Text = "Boomerang Service: Not Running";
                else
                    LabelServiceStatus.Text = "Boomerang Service: Running";
            }
            catch (InvalidOperationException)
            {
                LabelServiceStatus.Text = "Boomerang Service not found on" + Environment.MachineName;
            }
        }
        #endregion

    }
}
